﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Cars
{
    public static class WebUtils
    {
        public static void SmtpSend(string[] recipients, string from, string subject, string message, string smtpServer, int smtpPort, string smtpUsername, string smtpPassword)
        {

            try
            {
                // doing this one at a time for the SMS
                foreach (string recipient in recipients)
                {
                    // TODO: Add error handling for invalid arguments
                    MailMessage mailMsg = new MailMessage();

                    // To
                    mailMsg.To.Add(recipient);

                    // From
                    MailAddress mailAddress = new MailAddress(from);
                    mailMsg.From = mailAddress;

                    // Subject and Body
                    mailMsg.Subject = subject;
                    mailMsg.Body = message;

                    // Init SmtpClient and send
                    SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort);
                    if (!string.IsNullOrEmpty(smtpUsername))
                    {
                        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(smtpUsername, smtpPassword);
                        smtpClient.Credentials = credentials;
                        smtpClient.EnableSsl = true;
                    }

                    smtpClient.Send(mailMsg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public static string HttpGet(string URI)
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
            req.Method = "GET";
            req.ContentLength = 0;
            System.Net.WebResponse resp = req.GetResponse();
            if (resp == null) return null;
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        } 
    }
}
